#########################################################################   
#                                 INFO                                  #   
#########################################################################  

# PROJECT: Gender quotas in Tunisia's 2018 municipal elections
# PURPOSE: Show the correlation of Ennahda performance in 2011, 2014, 2018
# CREATED: January 2021 by Alexandra Blackman
# INPUTS:  education_by_sex_2014_del.xlsx
# OUTPUTS: education_by_sex_2014_mun.xlsx

#########################################################################   
#               SETUP                                                   #   
######################################################################### 

######## ENVIRONMENT

rm(list = ls()) 
setwd("~/Desktop/replication_what_men_want/")

######## PACKAGES

need <- c("openxlsx", "dplyr", "tidyr", "ggplot2", "ggthemes",
          "RColorBrewer","ggpubr") 
have <- need %in% rownames(installed.packages()) 
if(any(!have)) install.packages(need[!have]) 
invisible(lapply(need, library, character.only=T)) 

######## SET ALL TABLES TO SHOW "NA" BY DEFAULT

table = function (..., useNA = 'ifany') base::table(..., useNA = useNA)  

#########################################################################   
#               GGPLOT PARAMETERS                                       #   
######################################################################### 

# Colors
vid_low <- "#481567FF" #MHL
vid_med <- "#29AF7FFF" #FHL

#########################################################################   
#               LOAD DATA                                               #   
#########################################################################  

# Election variables by mun, wide
mun <- read.xlsx("data/elections_mun_new_variables_wide.xlsx")

# List-level election data ["list"]
load("data/quotas_list_clean.RData")



#########################################################################   
#                 FIGURES FOR THE PAPER                                 #   
######################################################################### 

#########################################################################   
#           Show distribution of strength measure (FIGURE 2)            #   
######################################################################### 
## histogram of strength measure
hist1 <- ggplot(list, aes(x=votes_per_nat_mean_2014_parl)) +
  geom_histogram(binwidth=5, colour="black", fill=vid_med) +
  labs(y = "Count", 
       x = "Relative Strength Measure",
       title = "") +
  coord_cartesian(xlim = c(-31, 50), ylim=c(0,400)) +
  theme_bw() +
  theme(axis.text.x=element_text(size=10),
        axis.title.x=element_text(size=12),
        axis.text.y=element_text(size=10),
        axis.title.y=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())

## histogram of standardized strength measure
hist2 <- ggplot(list, aes(x=sd_votes_per_nat_mean_2014_parl)) +
  geom_histogram(binwidth=.5, colour="black", fill=vid_low) + 
  labs(y = "Count", 
       x = "Relative Strength Measure (Standardized)",
       title = "") +
  coord_cartesian(xlim = c(-3, 4.5), ylim=c(0,400)) +
  theme_bw() +
  theme(axis.text.x=element_text(size=10),
        axis.title.x=element_text(size=12),
        axis.text.y=element_text(size=10),
        axis.title.y=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())

comb_hist <- ggarrange(hist1, hist2,
                       ncol = 2, nrow = 1)
comb_hist


png(file = "figures/iv_dist.png",
    width = 11, height = 5, units = 'in', res = 300)
comb_hist
dev.off()




###############################################################################    
#       PLOT OF ENNAHDA STRENGTH MEASURE ACROSS ELECTIONS  (FIGURE A.1)       #   
###############################################################################   


plot_diff1 <- ggplot(mun, aes(x = Ennahdha_nat_vote_diff_2011, y = Ennahdha_nat_vote_diff_2014_parl)) + 
  geom_text(aes(label=mun_en_2018), hjust=0, vjust=0, size=2) +
  geom_jitter() +
  #geom_smooth(span=.9) +
  geom_smooth(method = "lm", color = "red") + # using linear model ("lm")
  geom_abline(intercept = 0, slope = 1, color="black") +
  coord_cartesian(xlim = c(-.3, .5), ylim=c(-.3,.5)) +
  labs(y = "Ennahda Party Strength, 2014", 
       x = "Ennahda Party Strength, 2011",
       title = "") +
  theme_bw() +
  theme(axis.text.x=element_text(size=10),
        axis.title.x=element_text(size=12),
        axis.text.y=element_text(size=10),
        axis.title.y=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())

plot_diff1

plot_diff2 <- ggplot(mun, aes(x = Ennahdha_nat_vote_diff_2011, y = Ennahdha_nat_vote_diff_2018)) + 
  geom_text(aes(label=mun_en_2018), hjust=0, vjust=0, size=2) +
  geom_jitter() +
  #geom_smooth(span=.9) +
  geom_smooth(method = "lm", color = "red") + # using linear model ("lm")
  geom_abline(intercept = 0, slope = 1, color="black") +
  coord_cartesian(xlim = c(-.3, .5), ylim=c(-.3,.5)) +
  labs(y = "Ennahda Party Strength, 2018", 
       x = "Ennahda Party Strength, 2011",
       title = "") +
  theme_bw() +
  theme(axis.text.x=element_text(size=10),
        axis.title.x=element_text(size=12),
        axis.text.y=element_text(size=10),
        axis.title.y=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())

plot_diff2

plot_diff3 <- ggplot(mun, aes(x = Ennahdha_nat_vote_diff_2014_parl, y = Ennahdha_nat_vote_diff_2018)) + 
  geom_text(aes(label=mun_en_2018), hjust=0, vjust=0, size=2) +
  geom_jitter() +
  #geom_smooth(span=.9) +
  geom_smooth(method = "lm", color = "red") + # using linear model ("lm")
  geom_abline(intercept = 0, slope = 1, color="black") +
  coord_cartesian(xlim = c(-.3, .5), ylim=c(-.3,.5)) +
  labs(y = "Ennahda Party Strength, 2018", 
       x = "Ennahda Party Strength, 2014",
       title = "") +
  theme_bw() +
  theme(axis.text.x=element_text(size=10),
        axis.title.x=element_text(size=12),
        axis.text.y=element_text(size=10),
        axis.title.y=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())

plot_diff3



comb_diff <- ggarrange(plot_diff1, plot_diff2, plot_diff3,
                         labels = c("2011 & 2014", "2011 & 2018", "2014 & 2018"), 
                         ncol = 3, nrow = 1)
comb_diff


png(file = "figures/strength_appendix.png",
    width = 11, height = 5, units = 'in', res = 300)
comb_diff
dev.off()



####################################################################################    
#       PLOT OF PARTY STRENGTH MEASURE ACROSS PARTIES, 2014 & 2018  (FIGURE A.2)   #   
####################################################################################   

plot_ennahda <- ggplot(mun, aes(x = Ennahdha_nat_vote_diff_2014_parl, y = Ennahdha_nat_vote_diff_2018)) + 
  geom_text(aes(label=mun_en_2018), hjust=0, vjust=0, size=2) +
  geom_jitter() +
  #geom_smooth(span=.9) +
  geom_smooth(method = "lm", color = "red") + # using linear model ("lm")
  geom_abline(intercept = 0, slope = 1, color="black") +
  coord_cartesian(xlim = c(-.3, .5), ylim=c(-.3,.5)) +
  labs(y = "Ennahda Party Strength, 2018", 
       x = "Ennahda Party Strength, 2014",
       title = "") +
  theme_bw() +
  theme(axis.text.x=element_text(size=10),
        axis.title.x=element_text(size=12),
        axis.text.y=element_text(size=10),
        axis.title.y=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())

plot_ennahda

plot_nidaa <- ggplot(mun, aes(x = Nidaa_nat_vote_diff_2014_parl, y = Nidaa_nat_vote_diff_2018)) + 
  geom_text(aes(label=mun_en_2018), hjust=0, vjust=0, size=2) +
  geom_jitter() +
  #geom_smooth(span=.9) +
  geom_smooth(method = "lm", color = "red") + # using linear model ("lm")
  geom_abline(intercept = 0, slope = 1, color="black") +
  coord_cartesian(xlim = c(-.3, .5), ylim=c(-.3,.5)) +
  labs(y = "Nidaa Party Strength, 2018", 
       x = "Nidaa Party Strength, 2014",
       title = "") +
  theme_bw() +
  theme(axis.text.x=element_text(size=10),
        axis.title.x=element_text(size=12),
        axis.text.y=element_text(size=10),
        axis.title.y=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())

plot_nidaa


plot_fp <- ggplot(mun, aes(x = FP_grp_nat_vote_diff_2014_parl, y = FP_grp_nat_vote_diff_2018)) + 
  geom_text(aes(label=mun_en_2018), hjust=0, vjust=0, size=2) +
  geom_jitter() +
  #geom_smooth(span=.9) +
  geom_smooth(method = "lm", color = "red") + # using linear model ("lm")
  geom_abline(intercept = 0, slope = 1, color="black") +
  coord_cartesian(xlim = c(-.3, .5), ylim=c(-.3,.5)) +
  labs(y = "Popular Front Party Strength, 2018", 
       x = "Popular Front Party Strength, 2014",
       title = "") +
  theme_bw() +
  theme(axis.text.x=element_text(size=10),
        axis.title.x=element_text(size=12),
        axis.text.y=element_text(size=10),
        axis.title.y=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())

plot_fp


plot_current <- ggplot(mun, aes(x = Current_nat_vote_diff_2014_parl, y = Current_nat_vote_diff_2018)) + 
  geom_text(aes(label=mun_en_2018), hjust=0, vjust=0, size=2) +
  geom_jitter() +
  #geom_smooth(span=.9) +
  geom_smooth(method = "lm", color = "red") + # using linear model ("lm")
  geom_abline(intercept = 0, slope = 1, color="black") +
  coord_cartesian(xlim = c(-.3, .5), ylim=c(-.3,.5)) +
  labs(y = "Democratic Current Party Strength, 2018", 
       x = "Democratic Current Party Strength, 2014",
       title = "") +
  theme_bw() +
  theme(axis.text.x=element_text(size=10),
        axis.title.x=element_text(size=12),
        axis.text.y=element_text(size=10),
        axis.title.y=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())

plot_current



comb_parties <- ggarrange(plot_ennahda, plot_nidaa, plot_fp, plot_current,
                       labels = c("Ennahda", "Nidaa", "Popular Front","Democratic Current"), 
                       ncol = 2, nrow = 2)
comb_parties

png(file = "figures/parties_strength_appendix.png",
    width = 10, height = 8, units = 'in', res = 300)
comb_parties
dev.off()

